package com.atguigu.linkedlist;

public class SingleLinkedListDemo {

    public static void main(String[] args) {
        HeroNode hero1 = new HeroNode(1, "宋江", "及时雨");
        HeroNode hero2 = new HeroNode(2, "卢俊义", "玉麒麟");
        HeroNode hero3 = new HeroNode(3, "吴用", "智多星");
        HeroNode hero4 = new HeroNode(4, "林冲", "豹子头");
        HeroNode hero5 = new HeroNode(10, "小李广", "花荣");
        HeroNode hero6 = new HeroNode(7, "鲁智深", "花和尚");
        SingleLinkedList singleLinkedList = new SingleLinkedList();
        singleLinkedList.addByOrder(hero2);
        singleLinkedList.addByOrder(hero1);
        singleLinkedList.addByOrder(hero5);
        singleLinkedList.addByOrder(hero4);
        singleLinkedList.addByOrder(hero3);
        singleLinkedList.addByOrder(hero6);
        singleLinkedList.update(new HeroNode(1, "小明", "小明"));
        singleLinkedList.delete(101);
        singleLinkedList.list();
        System.out.println();
        singleLinkedList = singleLinkedList.reversePrint(singleLinkedList);
        singleLinkedList.list();
    }

}

class SingleLinkedList {
    // 创建头节点，头节点不要动
    private HeroNode head = new HeroNode(0, "", "");

    public HeroNode getHead() {
        return head;
    }

    public void add(HeroNode heroNode) {
        HeroNode temp = head;
        while (temp.next != null) {
            temp = temp.next;
        }
        temp.next = heroNode;
    }

    public void addByOrder(HeroNode heroNode) {
        if (head == null) {
            head = heroNode;
            return;
        }

        HeroNode temp = head;
        while (temp.next != null) {
            if (temp.next.no > heroNode.no) {
                break;
            }
            temp = temp.next;
        }

        if (temp.next == null && temp.no == heroNode.no) {
            System.out.printf("英雄编号%d已经存在，请换一个编号\n", heroNode.no);
        } else if (temp.no == heroNode.no) {
            System.out.printf("英雄编号%d已经存在，请换一个编号\n", heroNode.no);
        } else {
            heroNode.next = temp.next;
            temp.next = heroNode;
        }
    }

    public void update(HeroNode newHeronode) {
        if (head.next == null) {
            System.out.println("链表为空");
            return;
        }
        HeroNode targetNode = findNodeByNo(newHeronode.no);
        if (targetNode != null) {
            targetNode.name = newHeronode.name;
            targetNode.nickname = newHeronode.nickname;
        } else {
            System.out.println("没有找到");
        }
    }

    public void delete(int no) {
        if (head.next == null) {
            System.out.println("链表为空");
            return;
        }
        HeroNode prevNode = findPrevNodeByNo(no);
        if (prevNode != null && prevNode.next != null) {
            prevNode.next = prevNode.next.next;
        } else {
            System.out.println("没有找到要删除的编号");
        }
    }

    public void list() {
        if (head.next == null) {
            System.out.println("链表为空");
            return;
        }
        HeroNode temp = head;
        while (temp.next != null) {
            temp = temp.next;
            System.out.println(temp);
        }
    }

    public SingleLinkedList reversePrint(SingleLinkedList head) {
        if (head.getHead().next == null) {
            return null;
        }
        HeroNode prev = null;
        HeroNode curr = head.getHead().next;
        SingleLinkedList newList = new SingleLinkedList();
        while (curr != null) {
            HeroNode next = curr.next;
            curr.next = prev;
            prev = curr;
            curr = next;
        }
        newList.getHead().next = prev;
        return newList;
    }

    private HeroNode findNodeByNo(int no) {
        if (head.next == null) {
            return null;
        }
        HeroNode temp = head.next;
        while (temp != null) {
            if (temp.no == no) {
                return temp;
            }
            temp = temp.next;
        }
        return null;
    }

    private HeroNode findPrevNodeByNo(int no) {
        if (head.next == null) {
            return null;
        }
        HeroNode temp = head;
        while (temp.next != null) {
            if (temp.next.no == no) {
                return temp;
            }
            temp = temp.next;
        }
        return null;
    }
}

class HeroNode {
    public int no;
    public String name;
    public String nickname;
    public HeroNode next;

    public HeroNode(int no, String name, String nickname) {
        this.no = no;
        this.name = name;
        this.nickname = nickname;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickname='" + nickname + '\'' +
                '}';
    }
}
